package logic

import (
	"context"
	"pet-adopt-hub/models"

	"pet-adopt-hub/petrpc/internal/svc"
	"pet-adopt-hub/petrpc/petrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
	return &DetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailLogic) Detail(in *petrpc.DetailRequest) (*petrpc.DetailResponse, error) {
	// todo: add your logic here and delete this line
	var petdata models.PetData
	models.DB.Debug().
		Table("pets").
		Joins("left join styles on pets.style_id=styles.id").
		Joins("left join people on pets.person_id=people.id").
		Select("pets.*",
			"styles.name as StyleName",
			"people.name as PersonName").
		Where("pets.id=?", in.PetId).
		First(&petdata)

	return &petrpc.DetailResponse{
		Name:     petdata.Name,
		Sex:      petdata.Sex,
		Context:  petdata.Context,
		StyleId:  int64(petdata.StyleId),
		PersonId: int64(petdata.PersonId),
		PetId:    int64(petdata.ID),
	}, nil
}
